from django.db import models

from app.device.models import Devices
from dvadmin.utils.models import CoreModel, table_prefix
from dvadmin.system.models import SysDict, DeviceTypes, Department, Structures, RoadsInfos


class SamplePlan(CoreModel):
    name = models.CharField(max_length=20, verbose_name='预案名称', help_text="预案名称", null=True, blank=True)
    road = models.ForeignKey(RoadsInfos, db_column='roadid', on_delete=models.RESTRICT, verbose_name="路段ID",
                             help_text="路段ID", null=True, blank=True)
    plantype = models.ForeignKey(SysDict, db_column='plantype', on_delete=models.CASCADE, verbose_name="预案类别",
                                 help_text="预案类别", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'sampleplan'
        verbose_name = "专项预案表"
        verbose_name_plural = verbose_name


class MaterialAddress(CoreModel):
    name = models.CharField(max_length=20, verbose_name='存放点名称', help_text="存放点名称", null=True, blank=True)
    address = models.CharField(max_length=50, verbose_name='存放点位置', help_text="存放点位置", null=True, blank=True)
    lat = models.DecimalField(max_digits=13, decimal_places=10, verbose_name="精度", help_text="精度", null=True,
                              blank=True)
    lon = models.DecimalField(max_digits=13, decimal_places=10, verbose_name="纬度", help_text="纬度", null=True,
                              blank=True)

    class Meta:
        db_table = table_prefix + 'materialaddress'
        verbose_name = "物资存放位置表"
        verbose_name_plural = verbose_name


class Material(CoreModel):
    mname = models.CharField(max_length=20, verbose_name='物资名称', help_text="物资名称", null=True, blank=True)
    num = models.IntegerField(default=0, verbose_name='物资数量', help_text="物资数量", null=True, blank=True)
    unit = models.CharField(max_length=60, verbose_name="数量单位", help_text="数量单位", null=True, blank=True)
    pname = models.CharField(max_length=20, verbose_name="联系人", help_text="联系人", null=True, blank=True)
    phone = models.CharField(max_length=20, verbose_name="联系电话", help_text="联系电话", null=True, blank=True)
    plan = models.ForeignKey(SamplePlan, db_column='planid', on_delete=models.CASCADE, verbose_name="应急方案ID",
                             help_text="应急方案ID", null=True, blank=True)
    address = models.ForeignKey(MaterialAddress, db_column='addressid', on_delete=models.CASCADE,
                                verbose_name="物资存放点ID",
                                help_text="物资存放点ID", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'material'
        verbose_name = "物资信息表"
        verbose_name_plural = verbose_name


class AreaPlan(CoreModel):
    name = models.CharField(max_length=20, verbose_name='预案名称', help_text="预案名称", null=True, blank=True)
    startpoint = models.CharField(max_length=10, verbose_name='开始桩号', help_text="开始桩号", null=True, blank=True)
    endpoint = models.CharField(max_length=10, verbose_name='结束桩号', help_text="结束桩号", null=True, blank=True)
    struct = models.ForeignKey(Structures, db_column='structid', on_delete=models.CASCADE, verbose_name="构造物ID",
                               help_text="构造物ID", null=True, blank=True)
    yystartpoint = models.CharField(max_length=10, verbose_name='运营开始桩号', help_text="运营开始桩号", null=True, blank=True)
    yyendpoint = models.CharField(max_length=10, verbose_name='运营结束桩号', help_text="运营结束桩号", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'areaplan'
        verbose_name = "区域预案表"
        verbose_name_plural = verbose_name


class AreaPlanTypes(CoreModel):
    name = models.CharField(max_length=20, verbose_name='预案名称', help_text="预案名称", null=True, blank=True)
    startpoint = models.CharField(max_length=10, verbose_name='开始桩号', help_text="开始桩号", null=True, blank=True)
    plancode = models.CharField(max_length=18, verbose_name='预案编号', help_text="预案编号", null=True, blank=True)
    areaplan = models.ForeignKey(AreaPlan, db_column='areaplanid', on_delete=models.CASCADE, verbose_name="区域预案id",
                                 help_text="区域预案id", null=True, blank=True)
    plantype = models.ForeignKey(SysDict, db_column='plantype', on_delete=models.CASCADE, verbose_name="预案类别",
                                 help_text="预案类别", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'areaplantypes'
        verbose_name = "区域预案细分预案表"
        verbose_name_plural = verbose_name

    def _get_device_count(self):
        plan = self
        plan_device = DevicePlan.objects.filter(areaplantype=plan)
        return plan_device.count()

    deviceplancount = property(_get_device_count)


class DevicePlan(CoreModel):
    controlinfo = models.TextField(verbose_name='控制内容', help_text="控制内容", null=True, blank=True)
    num = models.IntegerField(default=0, verbose_name='执行顺序', help_text="执行顺序", null=True, blank=True)
    device = models.ForeignKey(Devices, db_column='deviceid', on_delete=models.CASCADE, verbose_name="设备ID",
                               help_text="设备ID", null=True, blank=True)
    areaplantype = models.ForeignKey(AreaPlanTypes, db_column='areaplantypeid', on_delete=models.CASCADE,
                                     verbose_name="区域预案细分预案ID",
                                     help_text="区域预案细分预案ID", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'deviceplan'
        verbose_name = "设备控制预案表"
        verbose_name_plural = verbose_name


class PlanOutpersion(CoreModel):
    coname = models.CharField(max_length=50, verbose_name='名称', help_text="名称", null=True, blank=True)
    persionname = models.CharField(max_length=20, verbose_name='人员名称', help_text="人员名称", null=True, blank=True)
    phone = models.CharField(max_length=11, verbose_name="联系电话", help_text="联系电话", null=True, blank=True)
    address = models.CharField(max_length=200, verbose_name="地址", help_text="地址", null=True, blank=True)
    plan = models.ForeignKey(SamplePlan, db_column='planid', on_delete=models.CASCADE, verbose_name="应急方案ID",
                             help_text="应急方案ID", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'plan_outpersion'
        verbose_name = "外部人员表"
        verbose_name_plural = verbose_name


class PlanInpersion(CoreModel):
    depname = models.CharField(max_length=50, verbose_name='部门名称', help_text="部门名称", null=True, blank=True)
    persionname = models.CharField(max_length=20, verbose_name='人员名称', help_text="人员名称", null=True, blank=True)
    phone = models.CharField(max_length=11, verbose_name="联系电话", help_text="联系电话", null=True, blank=True)
    post = models.CharField(max_length=20, verbose_name="岗位", help_text="岗位", null=True, blank=True)
    duty = models.CharField(max_length=20, verbose_name="职责", help_text="职责", null=True, blank=True)
    plan = models.ForeignKey(SamplePlan, db_column='planid', on_delete=models.CASCADE, verbose_name="应急方案ID",
                             help_text="应急方案ID", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'plan_inpersion'
        verbose_name = "内部人员表"
        verbose_name_plural = verbose_name
